//
//  PersonalInformationFormViewController.swift
//  iModern
//
//  Created by Ralf on 6/24/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class PersonalInformationFormViewController: FormViewController
{
    var contact = NSDictionary()

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.initBackButton()

        let image  = UIImage(named: "Save-Small.png")
        let button = UIBarButtonItem(image: image?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "publishButtonOnTap:")
        self.navigationItem.rightBarButtonItem = button


        self.contact = appDelegate.contactManager.contact;
        self.initializeForm()
    }

    func initializeForm()
    {
        let form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        form = XLFormDescriptor(title: " ")

        section = XLFormSectionDescriptor.formSectionWithTitle("")
        section.footerTitle = ""
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "alias", rowType: XLFormRowDescriptorTypeText, title: "昵称:")
        row.value = self.contact["alias"]
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "signature", rowType: XLFormRowDescriptorTypeText, title: "签名:")
        let signature = self.contact["signature"] as? String
        row.value = signature
        row.required = true
        section.addFormRow(row)

        self.form = form
    }

    @IBAction func publishButtonOnTap(sender: AnyObject)
    {
        let profile = NSMutableDictionary()

        profile["alias"] = self.formValues()["alias"]
        let signature = self.formValues()["signature"] as? String
        if signature == nil
        {
        }
        else
        {
            profile["signature"] = self.formValues()["signature"]
        }

        let rs = appDelegate.contactManager .updateProfile(profile as [NSObject:AnyObject])

        if rs == nil
        {
            self.alertNetworkConnectionException()

        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                self.view.endEditing(true)
                navigationController?.popToRootViewControllerAnimated(true)
            }
            alertErrorMessage(rs!.errorMessage)
        }
        self.view.endEditing(true)
        self.navigationController?.popViewControllerAnimated(true)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */

}
